﻿using System.Globalization;
using Xunit;

namespace Viyi.Util.Test.Utils
{
    // 由于测试目标代码是通过模板生成的，所以这里使用 ToInt32 作代表进行测试
    public class TestStringUtil_ToInt32
    {
        [Fact]
        public void TestToNullable()
        {
            Assert.Equal(12345, "12345".ToInt32());
            Assert.Equal(-987654, "-987654".ToInt32());
            Assert.Equal(int.MinValue, int.MinValue.ToString().ToInt32());
            Assert.Equal(int.MinValue, int.MinValue.ToString().ToInt32());
            Assert.Equal(null, "123abcd".ToInt32());
            Assert.Equal(null, (int.MaxValue + "1").ToInt32());
            Assert.Equal(null, (int.MinValue + "1").ToInt32());
        }

        [Fact]
        public void TestToNullableWithStyle()
        {
            Assert.Equal(12345, "12,345".ToInt32(NumberStyles.Integer | NumberStyles.AllowThousands));
            Assert.Equal(123456789, "123,456,789".ToInt32(NumberStyles.AllowThousands));
            Assert.Equal(0xabcdef, "abcdef".ToInt32(NumberStyles.HexNumber));
            Assert.Equal(int.MinValue, "80000000".ToInt32(NumberStyles.HexNumber));
            Assert.Equal(null, "123xyz".ToInt32(NumberStyles.HexNumber));
        }

        [Fact]
        public void TestToInteger()
        {
            Assert.Equal(12345, "12345".ToInt32(3));
            Assert.Equal(int.MinValue, int.MinValue.ToString().ToInt32(3));
            Assert.Equal(int.MinValue, int.MinValue.ToString().ToInt32(3));
            Assert.Equal(3, "123abcd".ToInt32(3));
            Assert.Equal(4, (int.MaxValue + "0").ToInt32(4));
            Assert.Equal(5, (int.MinValue + "0").ToInt32(5));
        }

        [Fact]
        public void TestToIntegerWithStyle()
        {
            Assert.Equal(12345, "12,345".ToInt32(
                NumberStyles.Integer | NumberStyles.AllowThousands, 7));
            Assert.Equal(0xabcdef, "abcdef".ToInt32(NumberStyles.HexNumber, 8));
            Assert.Equal(9, "123xyz".ToInt32(NumberStyles.HexNumber, 9));
        }
    }
}
